<template>
<div id="home">
  <nav-bar class="home-nav">
    <template v-slot:center>
      <div>购物街</div>
    </template>
  </nav-bar>
  <tab-control :titles="['流行', '新款', '精选']"
               class="tab-control"
               @tabClick="tabClick"
               ref="tabControl1"
               v-show="isShowTabControl" />
  <scroll class="cont"
          ref="scroll"
          :probeType="3"
          @scroll="backTopScroll"
          :pull-up-load="true"
          @pullingUp="loadMore">
    <home-swiper :banners="banners" @swiperImgLoad="swiperImgLoad" />
    <home-recommend-view :recommends="recommends" />
    <home-feature-view/>
    <tab-control :titles="['流行', '新款', '精选']"
                 @tabClick="tabClick"
                 ref="tabControl2" />
    <goods-list :goods="showGoods"  />
  </scroll>
<!--  vue3以下监听组件需要添加.native修饰符-->
  <back-top @click="backClick" v-show="isShowBackTop" />
</div>
</template>

<script>
import HomeSwiper from "./homeComps/HomeSwiper";
import HomeRecommendView from "./homeComps/HomeRecommendView";
import HomeFeatureView from "./homeComps/HomeFeatureView";

import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
import BackTop from "components/content/backTop/BackTop";

import {getHomeMultidata, getHomeGoods} from "network/home";

export default {
  name: "Home",
  components: {
    HomeSwiper,
    HomeRecommendView,
    HomeFeatureView,
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop
  },
  data(){
    return {
      banners: [],
      recommends: [],
      goods: {
        'pop': {page: 0,list: []},
        'new': {page: 0,list: []},
        'sell': {page: 0,list: []},
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffsetTop: 0,
      isShowTabControl: false,
      saveY: 0
    }
  },
  computed: {
    showGoods(){
      return this.goods[this.currentType].list
    }
  },
  created() {
    //请求多个数据
    this.getHomeMultidata();
    //请求首页数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')

  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.refresh()
  },
  deactivated() {
    // console.log(this.$refs.scroll.scroll.y)
    this.saveY = this.$refs.scroll.scroll.y
  },
  methods: {
    //自定义事件的方法
    tabClick(index){
      switch (index){
        case 0 :
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index
    },
    backClick(){
      this.$refs.scroll.scrollTo(0,0,500)
    },
    backTopScroll(position){
      //判断backTop是否显示
      this.isShowBackTop = (-position.y) > 1000

      //判断tabControl是否吸顶
      this.isShowTabControl = (-position.y) > this.tabOffsetTop
    },
    loadMore(){
      this.getHomeGoods(this.currentType)
      //让better-scroll插件重新计算content的高度
    },
    swiperImgLoad(){
     this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
      // console.log(this.tabOffsetTop);
    },

    //网络请求的方法
    getHomeMultidata(){
      //1.请求数据
      getHomeMultidata().then(res => {
        //把请求到的数据保存下来，否则res中的数据会被垃圾回收器自动回收
        this.banners = res.data.banner.list
        this.recommends = res.data.recommend.list
      })
    },
    getHomeGoods(type){
      const page = this.goods[type].page + 1
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page += 1
          //完成上拉加载更多
          this.$refs.scroll.refresh()
          this.$refs.scroll.finishPullUp()

        })
    },

  }
}
</script>

<style scoped>
#home{
  /*padding-top: 44px;*/
  height: 100vh;
  position: relative;
  font-weight: 500;
}
.home-nav{
  background-color: var(--color-tint);
  color: #fff;
  /*position: fixed;*/
  /*left: 0;*/
  /*right: 0;*/
  /*top: 0;*/
  /*z-index: 10;*/
}
.tab-control{
  position: relative;
  background-color: #fff;
  left: 0;
  top:-2px;
  right: 0;
  z-index: 9;
}
.cont{
  overflow: hidden;
  position: absolute;
  top: 43px;
  bottom: 49px;
}
</style>
